# 路由 + 视图函数在这里

# 导入蓝图,请求
from flask import Blueprint, jsonify, request

# 导入函数包

from .Cmod import *
from .Cmod_user import *
from .Cmod_express import *
# 蓝图
blue = Blueprint('user', __name__)


# 采纳问题的回答, 不能采纳自己的回答
@blue.route('/api/question/adopt', methods=['POST', 'GET'])
def adopt_answer():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 405
    data = request.json
    question_id = data.get('question_id')
    que = Questions.query.filter_by(question_id=question_id).first()
    answer_id = data.get('review_id')
    review_user_id = data.get('review_user_id')

    if que.question_answer < 2:
        que.question_answer += 1
        qrt = QuestionRelation(
            querelation_object=question_id,
            querelation_review=answer_id,
            querelation_number=que.question_answer,
        )
        db.session.add(qrt)
        db.session.commit()
    else:
        return {'message': '采纳数已达上线'}, 200
    get_adopt_exp(review_user_id)
    # 采纳回答
    return {'message': '采纳成功'}, 200


# 删除模块
@blue.route('/api/del', methods=['POST', 'GET'])
def delete_all():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 405
    data = request.json
    object_type = data.get('type')
    if object_type == "question":
        question_id = data.get('object_id')
        # 清除其链接的标签（清理标签关联表）
        delete_tagrelation(question_id, object_type)
        # 清除其链接的图片（清理图片关联表）
        delete_picture(question_id, object_type)
        # 清除其链接的评论（清理评论关联表）
        delete_small(question_id, object_type)
        # 清除其链接的回答（清理回答关联表）
        delete_question_relation(question_id)
        que = Questions.query.filter_by(question_id=question_id).first()
        db.session.delete(que)
        db.session.commit()
        print("问题清理完成")
        return {'message': '清理成功'}, 200
    elif object_type == "special":
        special_id = data.get('object_id')
        # 清除其链接的标签（清理标签关联表）
        delete_tagrelation(special_id, object_type)
        # 清理其下的动态
        spdy = SpDy.query.filter_by(spdy_father=special_id).all()
        for spdy_now in spdy:
            # 清除其链接的标签（清理标签关联表）
            delete_tagrelation(spdy_now.spdy_id, object_type)
            # 清除其链接的图片（清理图片关联表）
            delete_picture(spdy_now.spdy_id, object_type)
            # 清除其链接的评论（清理评论关联表）
            delete_small(spdy_now.spdy_id, object_type)
            db.session.delete(spdy_now)
            db.session.commit()
        spe = Special.query.filter_by(special_id=special_id).first()
        db.session.delete(spe)
        db.session.commit()
        print("专栏清理完成")
        return {'message': '清理成功'}, 200
    elif object_type == "spdy":
        spdy_id = data.get('object_id')
        # 清除其链接的标签（清理标签关联表）
        delete_tagrelation(spdy_id, object_type)
        # 清除其链接的图片（清理图片关联表）
        delete_picture(spdy_id, object_type)
        # 清除其链接的评论（清理评论关联表）
        delete_small(spdy_id, object_type)
        spdy = SpDy.query.filter_by(spdy_id=spdy_id).first()
        db.session.delete(spdy)
        db.session.commit()
        print("专栏下的动态清理完成")
        return {'message': '清理成功'}, 200
    elif object_type == "bookshelf":
        bookshelf_id = data.get('object_id')
        clear_bookshelf(bookshelf_id)
        # 清除其链接的标签（清理标签关联表）
        delete_tagrelation(bookshelf_id, object_type)
        # 清除其链接的图片（清理图片关联表）
        delete_picture(bookshelf_id, object_type)
        bsf = Bookshelf.query.filter_by(bookshelf_id=bookshelf_id).first()
        db.session.delete(bsf)
        db.session.commit()
        print("书架清理完成")
        return {'message': '清理成功'}, 200
    elif object_type == "dynamic":
        dynamic_id = data.get('object_id')
        # 清除其链接的标签（清理标签关联表）
        delete_tagrelation(dynamic_id, object_type)
        # 清除其链接的图片（清理图片关联表）
        delete_picture(dynamic_id, object_type)
        # 清除其链接的评论（清理评论关联表）
        delete_small(dynamic_id, object_type)
        dy = Dynamic.query.filter_by(dynamic_id=dynamic_id).first()
        db.session.delete(dy)
        db.session.commit()
        print("用户文章清理完成")
        return {'message': '清理成功'}, 200
    elif object_type == "review":
        review_id = data.get('object_id')
        rev = Review.query.filter_by(review_id=review_id).first()
        # 如果该评论为一级评论，其下的review_grade_top应该为空
        # 则删除所有review_grade_top为rev.review_id的评论
        if rev.review_grade_top is None:
            rev_list = Review.query.filter_by(review_grade_top=rev.review_id).all()
            if rev_list is not None:
                for rev_now in rev_list:
                    db.session.delete(rev_now)
        # 如果该评论为二级评论，就删除其本身,
        db.session.delete(rev)
        db.session.commit()
        print("评论清理完成")
        return {'message': '清理成功'}, 200
    else:
        return {'message': '未识别的type字符串'}, 400


# 将书籍添加进书架
@blue.route('/api/move/dynamic', methods=['POST', 'GET'])
def move_dynamic():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 405
    data = request.json
    user_id = data.get("id")
    bookrelation_bookshelf_id = data.get("bookshelf_id")
    dylist = data.get("dylist")
    for dy_id in dylist:
        # 先判断这篇文章有没有在书架中
        if validate_bookshelf_dynamic(bookrelation_bookshelf_id, dy_id):
            return {'message': f'{dy_id}已经存入该书架'}
        else:
            movein_dynamic(bookrelation_bookshelf_id, dy_id, user_id)
    return {'message': '添加成功'}, 200


# 将书籍移除书架
@blue.route('/api/move/bookshelf', methods=['POST', 'GET'])
def move_bookshelf():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 405
    data = request.json
    user_id = data.get("user_id")
    bookrelation_bookshelf_id = data.get("bookshelf_id")
    dylist = data.get("dylist")
    for dy_id in dylist:
        ql = Bookrelation.query.filter_by(
            bookrelation_bookshelf_id=bookrelation_bookshelf_id,
            bookrelation_dynamic_id=dy_id,
            bookrelation_user_id=user_id
        ).first()
        db.session.delete(ql)
    db.session.commit()
    return {'message': '移除成功'}, 200


@blue.route('/api/type/see', methods=['POST', 'GET'])
def query_type_see():
    if request.method == 'GET':
        return {'message': '错误的请求方式'}, 405
    data = request.get_json()
    object_id = data.get('object_id')
    object_type = data.get('object_type')
    if object_type == "question":
        ql = Questions.query.filter_by(question_id=object_id).first()
        ql.question_see += 1
        db.session.commit()
    elif object_type == "dynamic":
        ql = Dynamic.query.filter_by(dynamic_id=object_id).first()
        ql.dynamic_see += 1
        db.session.commit()
    elif object_type == "special":
        ql = Special.query.filter_by(special_id=object_id).first()
        ql.special_see += 1
        db.session.commit()
    elif object_type == "bookshelf":
        ql = Bookshelf.query.filter_by(bookshelf_id=object_id).first()
        ql.bookshelf_see += 1
        db.session.commit()
    else:
        return {'message': '未识别的object_type字符串'}, 200
    return {'message': '完成'}, 200


@blue.route('/')
def index():
    return "hello"